package com.hh.vietnameseproject.ui.changeplandetails

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.databinding.ActivityPlanDetailsBinding
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import com.hh.vietnameseproject.ui.login.ActivityController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class PlanDetailsActivity : AppCompatActivity() {
    lateinit var binding:ActivityPlanDetailsBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置通知栏透明
//        val decorView: View = window.decorView
//        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
//        window.statusBarColor = Color.TRANSPARENT

        binding = ActivityPlanDetailsBinding.inflate(layoutInflater)
        ActivityController.addList(this)
        setContentView(binding.root)
        binding.changeDetailsBack.setOnClickListener { onBackPressed() }
        binding.detailTitleText.setText(intent.getStringExtra("text"))
        binding.detailImage.setImageResource(intent.getIntExtra("image", R.drawable.vitnam))

        val wordsTotal = intent.getIntExtra("count",0)
        val id = intent.getIntExtra("id",1)
        var num = wordsTotal
        var wordCount = 1

        binding.numberPicker.maxValue = 30
        binding.numberPicker.minValue = 1
        binding.numberPicker.value = 1
        binding.everyDayNumbers.text = wordsTotal.toString()
        binding.numberPicker.setOnValueChangedListener { numberPicker, oldNum, newNum ->
            wordCount = (wordsTotal/newNum)
            binding.everyDayNumbers.text = wordCount.toString()
            num = newNum
        }
        binding.certainPlanBtn.setOnClickListener {
            GlobalScope.launch {
                val body = AllNetWork.updateSchedule(id.toString(),num.toString(),"0",wordCount.toString())
                val json = JSONObject(body.string())
                withContext(Dispatchers.Main){
                   Toast.makeText(this@PlanDetailsActivity,"${json.getString("message")}",Toast.LENGTH_SHORT).show()
                    startActivity(Intent(this@PlanDetailsActivity,MainActivity::class.java))
                }
            }



        }

    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityController.removeList(this)
    }

}